<template>
  <div style="line-height: 0;">
    <div style="margin-top: 15px; margin-bottom: 15px; line-height: 0; text-align: left;">
      <el-row>
        <label style="margin-left: 10px;">树苗名称：</label>
        <el-input style="width: 200px;" prefix-icon="el-icon-search" v-model="params.treename" placeholder="请输入树苗名称"></el-input>
        &nbsp;&nbsp;&nbsp;
        <label>商家名称：</label>
        <el-input style="width: 200px;" prefix-icon="el-icon-search" v-model="params.username" placeholder="请输入商家名称"></el-input>
        &nbsp;&nbsp;&nbsp;
        <el-button type="warning" round style="margin-left: 10px;" @click="findTree()">查询</el-button>
        &nbsp;&nbsp;&nbsp;
        <label>树苗年份：</label>
        <el-button type="warning" round style="margin-left: 10px;" @click="groupbyyeardesc">降序</el-button>
        <el-button type="warning" round style="margin-left: 10px;" @click="groupbyyearasc">升序</el-button>
        &nbsp;&nbsp;&nbsp;
        <label>树苗价格：</label>
        <el-button type="warning" round style="margin-left: 10px;" @click="groupbypricedesc">降序</el-button>
        <el-button type="warning" round style="margin-left: 10px;" @click="groupbypriceasc">升序</el-button>
        &nbsp;&nbsp;&nbsp;
        <el-button type="warning" round style="margin-left: 10px;" @click="reset()">重置</el-button>
      </el-row>
    </div>
    <div>
      <el-table
          :data="tableData"
          border
          style="width: 100%">
        <el-table-column prop="tree_id" v-if="false"></el-table-column>
        <el-table-column prop="username" label="商家名称" width="180"></el-table-column>
        <el-table-column prop="treename" label="树苗名称" width="180"></el-table-column>
        <el-table-column prop="treetime" label="树苗年份(年)" width="180"></el-table-column>
        <el-table-column prop="treeprice" label="树苗价格:(元)" width="180"></el-table-column>
        <el-table-column prop="treeproduce" label="树苗介绍"></el-table-column>
        <el-table-column prop="treetypename" label="树苗品种" width="180"></el-table-column>
        <el-table-column label="具体信息">
          <template slot-scope="scope">
            <el-button type="primary" @click="detail(scope.row)">查看详情</el-button>

          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block" style="text-align: center;">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="params.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

  </div>

</template>
<script>

import {findTree, loadTree} from "@/api/tree";
import {list} from "postcss";
import request from "@/utils/request";

export default {
  computed: {
    list() {
      return list
    }
  },

  data() {
    return {
      params: {
        username:'',
        treetypename:'',
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      total: 0,
      formLabelWidth: '190px',
    }
  },
  //在页面加载的时候，做一些事情
  created() {
    this.loadTree();
  },
  //定义一些页面上触发事件调用的方法
  methods:{
    loadTree(){
      if (localStorage.getItem("typename") === '') {
        loadTree(this.params).then(res => {
          if (res.code === '0'){
            this.tableData = res.data.list;
            this.total = res.data.total;
          } else{
            this.$message.error(res.msg)
          }
        })
      } else {
        this.params.treetypename = localStorage.getItem("typename")
        request.post("/tree/findbytreetype",this.params).then(res => {
          if (res.code === '0') {
            this.tableData = res.data.list;
            this.total = res.data.total;
          } else {
            this.$message.error(res.msg)
          }
        })
        localStorage.removeItem("typename");
      }

    },
    findTree(){
      findTree(this.params).then(res => {
        if (res.code === '0'){
          this.tableData = res.data.list;
          this.total = res.data.total;
        } else{
          this.$message.error(res.msg)
        }
      })
    },
    reset(){
      this.params = {
        pageNum: 1,
        pageSize: 10,
        treename:'',
        username: '',
      }
      this.loadTree();
    },
    groupbyyeardesc(){
      request.post("/tree/findbyyeardesc",this.params).then(res => {
        if (res.code === '0') {
          this.tableData = res.data.list
          this.total = res.data.total
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    groupbyyearasc(){
      request.post("/tree/findbyyearasc",this.params).then(res => {
        if (res.code === '0') {
          this.tableData = res.data.list
          this.total = res.data.total
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    groupbypricedesc(){
      request.post("/tree/findbypricedesc",this.params).then(res => {
        if (res.code === '0') {
          this.tableData = res.data.list
          this.total = res.data.total
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    groupbypriceasc(){
      request.post("/tree/findbypriceasc",this.params).then(res => {
        if (res.code === '0') {
          this.tableData = res.data.list
          this.total = res.data.total
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    detail(obj){
        let url = '/detailview?treeId=' + obj.treeId;
        location.href = url;
    },
    handleSizeChange(pageSize){
      this.params.pageSize = pageSize;
      this.loadTree();
    },
    handleCurrentChange(pageNum){
      this.params.pageNum = pageNum;
      this.loadTree();
    },
  }
}
</script>